<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="市" prop="town">
        <el-input
          v-model="queryParams.town"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼盘名称" prop="housesName">
        <el-input
          v-model="queryParams.housesName"
          placeholder="请输入楼盘名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼盘地址" prop="housesAddress">
        <el-input
          v-model="queryParams.housesAddress"
          placeholder="请输入楼盘地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总价" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="面积(平米)" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入面积(平米)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="室" prop="bedroom">
        <el-input
          v-model="queryParams.bedroom"
          placeholder="请输入室"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋类型" prop="houseType">
        <el-input
          v-model="queryParams.houseType"
          placeholder="请输入房屋类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="装修状况" prop="finishType">
        <el-input
          v-model="queryParams.finishType"
          placeholder="请输入装修状况"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="朝向" prop="orientation">
        <el-input
          v-model="queryParams.orientation"
          placeholder="请输入朝向"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房源名称" prop="houseName">
        <el-input
          v-model="queryParams.houseName"
          placeholder="请输入房源名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预计首付" prop="firstMoney">
        <el-input
          v-model="queryParams.firstMoney"
          placeholder="请输入预计首付"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建筑年代" prop="houseYear">
        <el-input
          v-model="queryParams.houseYear"
          placeholder="请输入建筑年代"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出租 出售" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入出租 出售"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house:house:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house:house:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house:house:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house:house:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="houseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房源编号 主键" align="center" prop="id" />
      <el-table-column label="楼盘图片" align="center" prop="defaultImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.defaultImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="楼盘名称" align="center" prop="housesName" />
      <el-table-column label="楼盘地址" align="center" prop="housesAddress" />
      <el-table-column label="房源名称" align="center" prop="houseName" />
      <el-table-column label="预计首付" align="center" prop="firstMoney" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="面积(平米)" align="center" prop="area" />
      <el-table-column label="房屋类型">
        <template slot-scope="scope">
          {{scope.row.houseType==1 ? '轻奢' : scope.row.houseType==2 ? '商务' : scope.row.houseType==3 ? '经典' : scope.row.houseType==4 ? '北欧' : '科技'}}
        </template>
      </el-table-column>
      <el-table-column label="装修状况">
        <template slot-scope="scope">
          {{scope.row.finishType==1 ? '毛坯' : scope.row.finishType==2 ? '简装' : scope.row.finishType==3 ? '精装' : '高定'}}
        </template>
      </el-table-column>
      <el-table-column label="朝向">
        <template slot-scope="scope">
          {{scope.row.orientation==1 ? '南' : scope.row.orientation==2 ? '北' : scope.row.orientation==3 ? '东' : '西'}}
        </template>
      </el-table-column>
      <el-table-column label="建筑年代" align="center" prop="houseYear" />
      <el-table-column label="入住时间" align="center" prop="comeTime" />
      <el-table-column label="省" align="center" prop="city" />
      <el-table-column label="市" align="center" prop="town" />
      <el-table-column label="区" align="center" prop="district" />
      <el-table-column label="室" align="center" prop="bedroom" />
      <el-table-column label="厅" align="center" prop="livingroom" />
      <el-table-column label="卫" align="center" prop="restroom" />
      <el-table-column label="厨房" align="center" prop="kitchen" />
      <el-table-column label="第几层" align="center" prop="totalStorey" />
      <el-table-column label="创建用户" align="center" prop="userId" />
      <el-table-column label="房产证是否满二年，用于计算税费">
        <template slot-scope="scope">
          {{scope.row.isTwoYear==1 ? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column label="是否为房东唯一住房，用于计算税费">
        <template slot-scope="scope">
          {{scope.row.isPersonOnly==1 ? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="myName" />
      <el-table-column label="联系电话" align="center" prop="myTel" />
      <el-table-column label="出售 出租">
        <template slot-scope="scope">
          {{scope.row.type==1 ? '出售' : '出租'}}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="房源自评" align="center" prop="myself" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house:house:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house:house:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="图片">
          <image-upload v-model="form.defaultImage"/>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="楼盘名称" prop="housesName"  >
              <el-input v-model="form.housesName" placeholder="请输入楼盘名称"  style="width: 100%;"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="楼盘地址" prop="housesAddress" >
              <el-input v-model="form.housesAddress" placeholder="请输入楼盘地址"  style="width: 100%;"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房源名称" prop="houseName"  >
              <el-input v-model="form.houseName" placeholder="请输入房源名称"  style="width: 100%;"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="预计首付" prop="firstMoney">
              <el-input v-model="form.firstMoney" placeholder="请输入预计首付" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="建筑年代" prop="houseYear">
              <el-input v-model="form.houseYear" placeholder="请输入建筑年代" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入住时间" prop="comeTime">
              <el-date-picker
                v-model="form.comeTime"
                type="datetime"
                placeholder="选择入住日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="总价" prop="totalPrice">
              <el-input v-model="form.totalPrice" placeholder="请输入总价" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="面积(平米)" prop="area">
              <el-input v-model="form.area" placeholder="请输入面积(平米)" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入住时间" prop="comeTime">
              <el-form-item label="出租 出售" prop="type">
                <template>
                  <el-radio v-model="form.type" label="1">出售</el-radio>
                  <el-radio v-model="form.type" label="2">出租</el-radio>
                </template>
              </el-form-item>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="省" prop="city">
              <el-input v-model="form.city" placeholder="请输入省" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="市" prop="town">
              <el-input v-model="form.town" placeholder="请输入市" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="区" prop="district">
              <el-input v-model="form.district" placeholder="请输入区" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="室" prop="bedroom">
              <el-input v-model="form.bedroom" placeholder="请输入室" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="厅" prop="livingroom">
              <el-input v-model="form.livingroom" placeholder="请输入厅" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="卫" prop="restroom">
              <el-input v-model="form.restroom" placeholder="请输入卫" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="厨房" prop="kitchen">
              <el-input v-model="form.kitchen" placeholder="请输入厨房" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="第几层" prop="totalStorey">
              <el-input v-model="form.totalStorey" placeholder="请输入第几层" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="共几层" prop="storey">
              <el-input v-model="form.storey" placeholder="请输入共几层" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房屋类型" prop="houseType">
              <el-select v-model="form.houseType" placeholder="请选择房屋类型">
                <el-option value="轻奢"></el-option>
                <el-option value="商务"></el-option>
                <el-option value="经典"></el-option>
                <el-option value="北欧"></el-option>
                <el-option value="科技"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="装修状况" prop="finishType">
              <template>
                <el-radio v-model="form.finishType" label="1">毛坯</el-radio>
                <el-radio v-model="form.finishType" label="2">简装</el-radio>
                <el-radio v-model="form.finishType" label="3">精装</el-radio>
                <el-radio v-model="form.finishType" label="4">高定</el-radio>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="朝向" prop="orientation">
              <template>
                <el-radio v-model="form.orientation" label="1">朝南</el-radio>
                <el-radio v-model="form.orientation" label="2">朝北</el-radio>
                <el-radio v-model="form.orientation" label="3">朝东</el-radio>
                <el-radio v-model="form.orientation" label="4">朝西</el-radio>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建用户" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入创建用户" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="房产证是否满二年，用于计算税费" prop="isTwoYear">
          <el-input v-model="form.isTwoYear" placeholder="请输入房产证是否满二年，用于计算税费" />
        </el-form-item>
        <el-form-item label="是否为房东唯一住房，用于计算税费" prop="isPersonOnly">
          <el-input v-model="form.isPersonOnly" placeholder="请输入是否为房东唯一住房，用于计算税费" />
        </el-form-item>

        <el-row>
          <el-col :span="8">
            <el-form-item label="姓名" prop="myName">
              <el-input v-model="form.myName" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="myTel">
              <el-input v-model="form.myTel" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房源自评" prop="myself">
              <el-input v-model="form.myself" placeholder="请输入房源自评" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHouse, getHouse, delHouse, addHouse, updateHouse } from "@/api/house/house";

export default {
  name: "House",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房源表格数据
      houseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        town: null,
        housesName: null,
        housesAddress: null,
        totalPrice: null,
        area: null,
        bedroom: null,
        houseType: null,
        finishType: null,
        orientation: null,
        houseName: null,
        firstMoney: null,
        houseYear: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        housesName: [
          { required: true, message: "楼盘名称不能为空", trigger: "blur" }
        ],
        housesAddress: [
          { required: true, message: "楼盘地址不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "纬度不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "经度不能为空", trigger: "blur" }
        ],
        myName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        myTel: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询房源列表 */
    getList() {
      this.loading = true;
      listHouse(this.queryParams).then(response => {
        this.houseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        snowflake: null,
        city: null,
        town: null,
        district: null,
        housesName: null,
        housesAddress: null,
        totalPrice: null,
        area: null,
        bedroom: null,
        livingroom: null,
        restroom: null,
        kitchen: null,
        totalStorey: null,
        storey: null,
        latitude: null,
        longitude: null,
        houseType: null,
        finishType: null,
        orientation: null,
        isTwoYear: null,
        isPersonOnly: null,
        houseName: null,
        firstMoney: null,
        houseYear: null,
        comeTime: null,
        myself: null,
        myName: null,
        myTel: null,
        type: null,
        createTime: null,
        userId: null,
        defaultImage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHouse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房源";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除房源编号为"' + ids + '"的数据项？').then(function() {
        return delHouse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house/house/export', {
        ...this.queryParams
      }, `house_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
